/*
 * Copyright 2007 Mojo Chen
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

var linkxdMain = {
	
	xdlink:'http://linkxd.com',
	xdtitle:'快樂是我們連結的理由',
	xdtitleEn:'Have a Nice Day',
	xdpng:'http://linkxd.com/images/linkxd.png',
	xdpngChrome:'chrome://linkxd/skin/bar.png',
	pngCutChrome:'chrome://linkxd/skin/edit-cut.png',
	xdexternal:'http://linkxd.com/images/external.png',
	xdChromeExternal:'chrome://linkxd/skin/external.png',
	
	dumpToConsole: true,
    enableAlerts: false,
	
	consoleService : Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService),
    promptService : Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService),
  		
	log:function(title, msg){
		
		if(!this.eol){
			this.eol = this.getEol();
		}
        
        if(this.dumpToConsole) {
            this.consoleService.logStringMessage("linkxd:" + title  + this.eol + msg);
        }
        
       // if(this.enableAlerts) {
            //this.promptService.alert(window,"linkxd",title + ':'+message);
       // }
  	},
  	
	openBottomBar: function(doOpen) {
		var bottomBar = document.getElementById("linkxd-outerbox");
		var mylinkSplinter = document.getElementById("linkxdSplit");
		var theContent = document.getElementById("appcontent");
		theContent.appendChild(mylinkSplinter);
		theContent.appendChild(bottomBar);
		if(doOpen){
			bottomBar.setAttribute('collapsed', false );
			mylinkSplinter.setAttribute('collapsed', false );
        }else{
			bottomBar.setAttribute('collapsed', !bottomBar.collapsed );
			mylinkSplinter.setAttribute('collapsed', !mylinkSplinter.collapsed );
        }
		document.getElementById("linkxdMidasFrame").setAttribute("src", "chrome://linkxd/content/editor.xul");
    },
    
    getString:function(key){
    	var strings = document.getElementById("linkxd-strings");
    	return strings.getString(key);
    },
    
    getPlatform: function() {
        var platform = navigator.platform.toLowerCase();
        if(platform.indexOf('win') != -1) return 'win'; // win
        else if(platform.indexOf('mac') != -1) return 'mac'; // mac
        else return 'other'; // other (nix)
    },
    
    getEol: function() {
        switch(this.getPlatform()) {
            case 'win': return '\r\n';
            case 'mac': return '\r';
            case 'other': return '\n';
            default: return '<eol>';
        }
    }, 
    
    paste:function(text){
    	//alert(text);
    	try{
    		// this is needed to generally allow usage of components in javascript
    		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    		var oClipBoard = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
    		oClipBoard.copyString(text);
    	}catch(err){
			alert(err);
		}
    }
}

var linkxdWiki = {

  getLink: function(url,str){
    return "[" + url + " " + str + "]";
  },
  
  getQuote: function(url,title,str){
    return linkxdMain.getString("quote") + " [" + url + " " + title + "]:\r\n<PRE>"+str+'</PRE>';
  }
  
}

var linkxdHtml = {

  getLink: function(url,str){
     return '<a href="'+url+'">'+str+'</a><img style="border:0;vertical-align:middle;" src="http://linkxd.com/images/external.png"/>';
  },
  
  getLinkXd: function(){
     return '<a href="http://linkxd.com/">'+linkxdMain.xdtitle+'</a><img style="border:0;vertical-align:middle;" src="http://linkxd.com/images/linkxd.png"/>';
  },
  
  getLinkXdEn: function(){
     return '<a href="http://linkxd.com/">'+linkxdMain.xdtitleEn+'</a><img style="border:0;vertical-align:middle;" src="http://linkxd.com/images/linkxd.png"/>';
  },
  
  getQuote: function(url,title,str){
   return linkxdMain.getString("quote") + ' <a href="' + url +'">' + title + '</a>:\r\n<PRE>'+str+'</PRE>';
  }
  
}

var linkxdBB = {

  getLink: function(url,str){
     return '[url='+url+']'+str+'[/url]';
  },
  
  getQuote: function(url,title,str){
   return linkxdMain.getString("quote") + ' [url=' + url +']' + title + '[/url] : \r\n[quote]'+str+'[/quote]';
  }
  
}

var linkxdBK = {
	putHemi: function(title,url,str){
		var target = 'http://www.hemidemi.com/user_bookmark/new?url='+encodeURIComponent(url)+'&title=';
		target = target+encodeURIComponent(title)+'&description='+encodeURIComponent(str);
		window.open(target);
	},
  
   putFunp: function(title,url,str){
		var target = 'http://funp.com/push/submit/?popup&url='+encodeURIComponent(url)+'&t=';
		if(str != null && str.length > 0){
			target = target+encodeURIComponent(str);
		} else {
			target = target+encodeURIComponent(title);
		}
		window.open(target,'FunP.com', 'height=500,width=760,scrollbars,resizable');
	}
}
